package org.qf.web;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.qf.entity.SmbmsProvider;
import org.qf.service.SmbmsProviderService;

@Slf4j
@Controller
public class SmbmsProviderController {

    @Autowired
    SmbmsProviderService smbmsProviderService;
    @RequestMapping("/showProvideList")
    @PostMapping
    public String showProvideList(@RequestParam(defaultValue = "1") int currPage, String name, Model model) {
        SmbmsProvider smbmsProvide = new SmbmsProvider();
        smbmsProvide.setProName(name);
        PageInfo<SmbmsProvider> pageInfo = smbmsProviderService.showProvideList(currPage,smbmsProvide);
        if (pageInfo.getList().size() > 0) {
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("name", name);
            return "providerList";
        } else {
            return "error";
        }
    }
    /**
     * 点击跳转页面
     *
     * @return
     */
    @RequestMapping("/addProvider")
    public String addProvider() {
        return "providerAdd";
    }

    /**
     * 添加供应商
     * @param smbmsProvide
     * @return
     */
    @RequestMapping("/addProvider1")
    public String addProvider1(SmbmsProvider smbmsProvide) {
        int i = smbmsProviderService.addProvider(smbmsProvide);
        if (i > 0) {
            return "redirect:/showProvideList";
        } else {
            return "redirect:/addProvider";
        }
    }

    /**
     * 通过id查找供应商信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/viewProvider")
    public String viewProvider(int id, Model model) {
        SmbmsProvider smbmsProvider = smbmsProviderService.showProviderById(id);
        if (smbmsProvider != null) {
            model.addAttribute("p", smbmsProvider);
            return "providerView";
        } else {
            return "error";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/updateProviderView")
    public String updateProviderView(int id, Model model) {
        SmbmsProvider smbmsProvider = smbmsProviderService.showProviderById(id);
        if (smbmsProvider != null) {
            model.addAttribute("p", smbmsProvider);
            return "providerUpdate";
        } else {
            return "error";
        }
    }

    /**
     * 修改
     *
     * @param smbmsProvider
     * @return
     */
    @RequestMapping("/updateProvider")
    public String updateProvider(SmbmsProvider smbmsProvider) {
        int i = smbmsProviderService.updateProvider(smbmsProvider);
        if (i > 0) {
            return "redirect:/showProvideList";
        } else {
            return "error";
        }
    }

    /**
     * 删除供应商
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteProvider")
    public String deleteProvider(int id) {
        int i = smbmsProviderService.deleteProvider(id);
        if (i > 0) {
            return "redirect:/showProvideList";
        } else {
            return "error";
        }
    }
}
